import ConfigParser
import string
import random
import glob
import re
from PyQt4 import QtGui,QtCore

class BuildDataStore(object):
    def __init__(self):
        super(BuildDataStore,self).__init__()
        self.openmodels={}
 
    def GetBuildDataModel(self,ChampID):
        if self.openmodels.has_key(ChampID):
            return self.openmodels[ChampID]
        else:
            build=BuildDataModel(ChampID)
            self.openmodels[ChampID]=build
            return self.openmodels[ChampID]

    def GetAvailableChampSaves(self):
        savelist=glob.glob("saves/builds/*.sav")
        for i in range(len(savelist)):
            m=re.search('(\d+)',savelist[i])
            if m:
                savelist[i]=m.groups(1)[0]

        return savelist

class RecommendedDataStore(object):
    def __init__(self):
        super(RecommendedDataStore,self).__init__()
        self.RecommendedModel=RecommendedDataModel()

    def GetRecommendedDataModel(self):
        return self.RecommendedModel

class MasteriesDataStore(object):
    def __init__(self):
        super(MasteriesDataStore,self).__init__()
        self.MasteriesModel=MasteriesDataModel()

    def GetMasteriesDataModel(self):
        return self.MasteriesModel


class MasteriesDataModel(QtCore.QAbstractListModel):
    def __init__(self):
        super(MasteriesDataModel,self).__init__()
        self.config=ConfigParser.SafeConfigParser()
        print "MasteriesdataModel Created"
        self.config.read("saves/masteries.sav")
        self.CurrentMasteries=""
        self.UpdateMasteriesIndex()

    def UpdateMasteriesIndex(self):
        MasteriesList={}
        self.MasteriesIndex=[]
        for i in self.config.sections():
            MasteriesList[int(self.config.get(i,"position"))]=i
        for i in sorted(MasteriesList.keys()):
            self.MasteriesIndex.append(MasteriesList[i])

    def UpdatePosition(self):
        pos = 1
        for i in self.MasteriesIndex:
            self.config.set(i,"position",str(pos))
            pos +=1
        self.saveConfig()

    def rowCount(self,parent = QtCore.QModelIndex()):
        return len(self.config.sections())


    def data(self,index,role):
        if role == QtCore.Qt.DisplayRole:
            MID=self.MasteriesIndex[index.row()]
            return self.config.get(MID,"name")

    def flags(self, index):
        return (QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable)

    def insertRows(self,position,rows,name,parent = QtCore.QModelIndex()):
        self.beginInsertRows(parent, position, position + rows - 1)
        MID = GenerateRandomString()
        position = self.getLastPosition()
        self.config.add_section(MID)
        self.config.set(MID,"position",str(position))
        self.config.set(MID,"name",str(name))
        # offensive
        self.config.set(MID,"SummonersWrath","0")
        self.config.set(MID,"BruteForce","0")
        self.config.set(MID,"MentalForce","0")
        self.config.set(MID,"Butcher","0")
        self.config.set(MID,"Alacrity","0")
        self.config.set(MID,"Sorcery","0")
        self.config.set(MID,"Demolitionist","0")
        self.config.set(MID,"Deadliness","0")
        self.config.set(MID,"WeaponsExpertise","0")
        self.config.set(MID,"ArcaneKnowledge","0")
        self.config.set(MID,"Havoc","0")
        self.config.set(MID,"Lethality","0")
        self.config.set(MID,"Vampirism","0")
        self.config.set(MID,"Blast","0")
        self.config.set(MID,"Sunder","0")
        self.config.set(MID,"Archmage","0")
        self.config.set(MID,"Executioner","0")
        # defensive
        self.config.set(MID,"SummonersResolve","0")
        self.config.set(MID,"Resistance","0")
        self.config.set(MID,"Hardiness","0")
        self.config.set(MID,"ToughSkin","0")
        self.config.set(MID,"Durability","0")
        self.config.set(MID,"Vigor","0")
        self.config.set(MID,"Indomitable","0")
        self.config.set(MID,"VeteransScars","0")
        self.config.set(MID,"Evasion","0")
        self.config.set(MID,"BladedArmor","0")
        self.config.set(MID,"SiegeCommander","0")
        self.config.set(MID,"Initiator","0")
        self.config.set(MID,"Enlightenment","0")
        self.config.set(MID,"HonorGuard","0")
        self.config.set(MID,"Mercenary","0")
        self.config.set(MID,"Juggernaut","0")
        # utility
        self.config.set(MID,"SummonersInsight","0")
        self.config.set(MID,"GoodHands","0")
        self.config.set(MID,"ExpandedMind","0")
        self.config.set(MID,"ImprovedRecall","0")
        self.config.set(MID,"Swiftness","0")
        self.config.set(MID,"Meditation","0")
        self.config.set(MID,"Scout","0")
        self.config.set(MID,"Greed","0")
        self.config.set(MID,"Transmutation","0")
        self.config.set(MID,"RunicAffinity","0")
        self.config.set(MID,"Wealth","0")
        self.config.set(MID,"Awareness","0")
        self.config.set(MID,"Sage","0")
        self.config.set(MID,"StrengthOfSpirit","0")
        self.config.set(MID,"Intelligence","0")
        self.config.set(MID,"Mastermind","0")
        self.endInsertRows()
        self.saveConfig()
        self.UpdateMasteriesIndex()
        return True

    def getLastPosition(self):
        return len(self.MasteriesIndex)+1

    def removeRows(self, position, rows, parent = QtCore.QModelIndex()):
        self.beginRemoveRows(parent, position, position + rows - 1)
        MID=self.MasteriesIndex[position]
        self.config.remove_section(MID)
        del self.MasteriesIndex[position]
        self.endRemoveRows()
        self.UpdatePosition()
        return True

    def getMID(self,pos):
        return self.MasteriesIndex[pos]
        
    def setData(self, index, value, role = QtCore.Qt.EditRole):
        if role == QtCore.Qt.EditRole:
            #row = index.row()
            #name=value
            MID=self.MasteriesIndex[index.row()]
            self.config.set(MID,"name",str(value.toString()))
            self.saveConfig()
            return True
        return False

    def saveConfig(self):
        FILE=open("saves/masteries.sav","w")
        self.config.write(FILE)
        FILE.close()
        print "SavedDataStore: saveConfig: Config written: Masteries.sav"

    def saveMasteries(self,mastery):
        print "SavedDataStore: saveMasteries: 'mastery': ",mastery
        for key,value in sorted(mastery.iteritems()):
            if value:
                self.config.set(self.CurrentMID,key,str(value))
            else:
                self.config.set(self.CurrentMID,key,"0")
        self.saveConfig()

    def loadMasteries(self,MID):
        mastery = {}
        mastery["SummonersWrath"]=self.config.get(MID,"SummonersWrath")
        mastery["BruteForce"]=self.config.get(MID,"BruteForce")
        mastery["MentalForce"]=self.config.get(MID,"MentalForce")
        mastery["Butcher"]=self.config.get(MID,"Butcher")
        mastery["Alacrity"]=self.config.get(MID,"Alacrity")
        mastery["Sorcery"]=self.config.get(MID,"Sorcery")
        mastery["Demolitionist"]=self.config.get(MID,"Demolitionist")
        mastery["Deadliness"]=self.config.get(MID,"Deadliness")
        mastery["WeaponsExpertise"]=self.config.get(MID,"WeaponsExpertise")
        mastery["ArcaneKnowledge"]=self.config.get(MID,"ArcaneKnowledge")
        mastery["Havoc"]=self.config.get(MID,"Havoc")
        mastery["Lethality"]=self.config.get(MID,"Lethality")
        mastery["Vampirism"]=self.config.get(MID,"Vampirism")
        mastery["Blast"]=self.config.get(MID,"Blast")
        mastery["Sunder"]=self.config.get(MID,"Sunder")
        mastery["Archmage"]=self.config.get(MID,"Archmage")
        mastery["Executioner"]=self.config.get(MID,"Executioner")
        # defensive
        mastery["SummonersResolve"]=self.config.get(MID,"SummonersResolve")
        mastery["Resistance"]=self.config.get(MID,"Resistance")
        mastery["Hardiness"]=self.config.get(MID,"Hardiness")
        mastery["ToughSkin"]=self.config.get(MID,"ToughSkin")
        mastery["Durability"]=self.config.get(MID,"Durability")
        mastery["Vigor"]=self.config.get(MID,"Vigor")
        mastery["Indomitable"]=self.config.get(MID,"Indomitable")
        mastery["VeteransScars"]=self.config.get(MID,"VeteransScars")
        mastery["Evasion"]=self.config.get(MID,"Evasion")
        mastery["BladedArmor"]=self.config.get(MID,"BladedArmor")
        mastery["SiegeCommander"]=self.config.get(MID,"SiegeCommander")
        mastery["Initiator"]=self.config.get(MID,"Initiator")
        mastery["Enlightenment"]=self.config.get(MID,"Enlightenment")
        mastery["HonorGuard"]=self.config.get(MID,"HonorGuard")
        mastery["Mercenary"]=self.config.get(MID,"Mercenary")
        mastery["Juggernaut"]=self.config.get(MID,"Juggernaut")
        # utility
        mastery["SummonersInsight"]=self.config.get(MID,"SummonersInsight")
        mastery["GoodHands"]=self.config.get(MID,"GoodHands")
        mastery["ExpandedMind"]=self.config.get(MID,"ExpandedMind")
        mastery["ImprovedRecall"]=self.config.get(MID,"ImprovedRecall")
        mastery["Swiftness"]=self.config.get(MID,"Swiftness")
        mastery["Meditation"]=self.config.get(MID,"Meditation")
        mastery["Scout"]=self.config.get(MID,"Scout")
        mastery["Greed"]=self.config.get(MID,"Greed")
        mastery["Transmutation"]=self.config.get(MID,"Transmutation")
        mastery["RunicAffinity"]=self.config.get(MID,"RunicAffinity")
        mastery["Wealth"]=self.config.get(MID,"Wealth")
        mastery["Awareness"]=self.config.get(MID,"Awareness")
        mastery["Sage"]=self.config.get(MID,"Sage")
        mastery["StrengthOfSpirit"]=self.config.get(MID,"StrengthOfSpirit")
        mastery["Intelligence"]=self.config.get(MID,"Intelligence")
        mastery["Mastermind"]=self.config.get(MID,"Mastermind")
        mastery['MID']=MID
        mastery['position']=self.config.get(MID,"position")
        mastery['name']=self.config.get(MID,"name")
        self.CurrentMID=MID
        return mastery
        
class BuildDataModel(QtCore.QAbstractListModel):
    def __init__(self,ChampID, parent = None):
        super(BuildDataModel,self).__init__()
        self.ChampID=ChampID
        self.config=ConfigParser.SafeConfigParser()

        print "BuildDataModel Created ->",ChampID
        self.config.read("saves/builds/%s.sav" % (self.ChampID))
        self.CurrentBID=""
        self.UpdateBuildIndex()

    def UpdateBuildIndex(self):
        buildlist={}
        self.BuildIndex=[]
        for i in self.config.sections():
            buildlist[int(self.config.get(i,"position"))]=i
        for i in sorted(buildlist.keys()):
            self.BuildIndex.append(buildlist[i])
#        self.BuildIndex=self.config.sections()

    def UpdatePosition(self):
        pos = 1
        for i in self.BuildIndex:
            self.config.set(i,"position",str(pos))
            pos += 1
        self.saveConfig()

    def rowCount(self,parent = QtCore.QModelIndex()):
        return len(self.config.sections())

    def data(self,index,role):
        if role == QtCore.Qt.DisplayRole:
#            print "row: ",index.row()
#            print "len buildindex, ",len(self.BuildIndex)
            BID=self.BuildIndex[index.row()]
            return self.config.get(BID,"name")

    def flags(self, index):
        return (QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable)

    def insertRows(self,position,rows,name,parent = QtCore.QModelIndex()):
        self.beginInsertRows(parent, position, position + rows - 1)
        BID = GenerateRandomString()
        position = self.getLastPosition()
        self.config.add_section(BID)
        self.config.set(BID,"name",str(name))
        self.config.set(BID,"position",str(position))
        self.config.set(BID,"Item1","None")
        self.config.set(BID,"Item2","None")
        self.config.set(BID,"Item3","None")
        self.config.set(BID,"Item4","None")
        self.config.set(BID,"Item5","None")
        self.config.set(BID,"Item6","None")
        self.endInsertRows()
        self.saveConfig()
        self.UpdateBuildIndex()
        return True

    def getLastPosition(self):
        return len(self.BuildIndex)+1
        
    def removeRows(self, position, rows, parent = QtCore.QModelIndex()):
        self.beginRemoveRows(parent, position, position + rows - 1)
        BID=self.BuildIndex[position]
        self.config.remove_section(BID)
        del self.BuildIndex[position]
        self.endRemoveRows()
        self.UpdatePosition()
        return True

    def getBID(self,pos):
        return self.BuildIndex[pos]
        
    def setData(self, index, value, role = QtCore.Qt.EditRole):
        if role == QtCore.Qt.EditRole:
            #row = index.row()
            #name=value
            BID=self.BuildIndex[index.row()]
            self.config.set(BID,"name",str(value.toString()))
            self.saveConfig()
            return True
        return False

    def saveConfig(self):
        FILE=open("saves/builds/%s.sav" % (self.ChampID),"w")
        self.config.write(FILE)
        FILE.close()
        print "SavedDataStore: saveConfig: Config written: "

    def saveBuild(self,build):
        print "SavedDataStore: saveBuild: 'build': ",build
        for key,value in sorted(build.iteritems()):
            if value:
                self.config.set(self.CurrentBID,"Item%s" % key,str(value.ID))
            else:
                self.config.set(self.CurrentBID,"Item%s" % key,"None")
        self.saveConfig()

    def loadBuild(self,BID):
        build = {}
        build['Item1']=self.config.get(BID,"Item1")
        build['Item2']=self.config.get(BID,"Item2")
        build['Item3']=self.config.get(BID,"Item3")
        build['Item4']=self.config.get(BID,"Item4")
        build['Item5']=self.config.get(BID,"Item5")
        build['Item6']=self.config.get(BID,"Item6")
        build['Name']=self.config.get(BID,"name")
        build['BID']=BID
        self.CurrentBID=BID
        return build

def GenerateRandomString(size=20, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.choice(chars) for i in range(size))

###############
#### Runes ####
###############

class RunesDataStore(object):
    def __init__(self):
        super(RunesDataStore,self).__init__()
        self.RunesModel=RunesDataModel()

    def GetRunesDataModel(self):
        return self.RunesModel

class RunesDataModel(QtCore.QAbstractListModel):
    def __init__(self):
        super(RunesDataModel,self).__init__()
        self.config=ConfigParser.SafeConfigParser()
        print "RunesdataModel Created"
        self.config.read("saves/runes.sav")
        self.CurrentRunes=""
        self.UpdateRunesIndex()

    def UpdateRunesIndex(self):
        RunesList={}
        self.RunesIndex=[]
        for i in self.config.sections():
            RunesList[int(self.config.get(i,"position"))]=i
        for i in sorted(RunesList.keys()):
            self.RunesIndex.append(RunesList[i])

    def UpdatePosition(self):
        pos = 1
        for i in self.RunesIndex:
            self.config.set(i,"position",str(pos))
            pos +=1
        self.saveConfig()

    def rowCount(self,parent = QtCore.QModelIndex()):
        return len(self.config.sections())

    def data(self,index,role):
        if role == QtCore.Qt.DisplayRole:
            RID=self.RunesIndex[index.row()]
            return self.config.get(RID,"name")

    def flags(self, index):
        return (QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable)

    def insertRows(self,position,rows,name,parent = QtCore.QModelIndex()):
        self.beginInsertRows(parent, position, position + rows - 1)
        RID = GenerateRandomString()
        position = self.getLastPosition()
        self.config.add_section(RID)
        self.config.set(RID,"position",str(position))
        self.config.set(RID,"name",str(name))
        # marks
        self.config.set(RID,"mark1","None")
        self.config.set(RID,"mark2","None")
        self.config.set(RID,"mark3","None")
        self.config.set(RID,"mark4","None")
        self.config.set(RID,"mark5","None")
        self.config.set(RID,"mark6","None")
        self.config.set(RID,"mark7","None")
        self.config.set(RID,"mark8","None")
        self.config.set(RID,"mark9","None")
        # seals
        self.config.set(RID,"seal1","None")
        self.config.set(RID,"seal2","None")
        self.config.set(RID,"seal3","None")
        self.config.set(RID,"seal4","None")
        self.config.set(RID,"seal5","None")
        self.config.set(RID,"seal6","None")
        self.config.set(RID,"seal7","None")
        self.config.set(RID,"seal8","None")
        self.config.set(RID,"seal9","None")
        # glyphs
        self.config.set(RID,"glyph1","None")
        self.config.set(RID,"glyph2","None")
        self.config.set(RID,"glyph3","None")
        self.config.set(RID,"glyph4","None")
        self.config.set(RID,"glyph5","None")
        self.config.set(RID,"glyph6","None")
        self.config.set(RID,"glyph7","None")
        self.config.set(RID,"glyph8","None")
        self.config.set(RID,"glyph9","None")
        # quintessence
        self.config.set(RID,"quintessence1","None")
        self.config.set(RID,"quintessence2","None")
        self.config.set(RID,"quintessence3","None")
        self.endInsertRows()
        self.saveConfig()
        self.UpdateRunesIndex()
        return True

    def getLastPosition(self):
        return len(self.RunesIndex)+1

    def removeRows(self, position, rows, parent = QtCore.QModelIndex()):
        self.beginRemoveRows(parent, position, position + rows - 1)
        RID=self.RunesIndex[position]
        self.config.remove_section(RID)
        del self.RunesIndex[position]
        self.endRemoveRows()
        self.UpdatePosition()
        return True

    def getRID(self,pos):
        return self.RunesIndex[pos]

    def setData(self, index, value, role = QtCore.Qt.EditRole):
        if role == QtCore.Qt.EditRole:
            #row = index.row()
            #name=value
            RID=self.RunesIndex[index.row()]
            self.config.set(RID,"name",str(value.toString()))
            self.saveConfig()
            return True
        return False

    def saveConfig(self):
        FILE=open("saves/runes.sav","w")
        self.config.write(FILE)
        FILE.close()
        print "SavedDataStore: saveConfig: Config written: runes.sav"

    def saveRunes(self,runes):
        print "SavedDataStore: saveRunes: 'runes': ",runes
        for key,value in sorted(runes.iteritems()):
            if value:
                self.config.set(self.CurrentRID,key,str(value))
            else:
                self.config.set(self.CurrentRID,key,"None")
        self.saveConfig()

    def loadRunes(self,RID):
        runes = {}
        runes["mark1"]=self.config.get(RID,"mark1")
        runes["mark2"]=self.config.get(RID,"mark2")
        runes["mark3"]=self.config.get(RID,"mark3")
        runes["mark4"]=self.config.get(RID,"mark4")
        runes["mark5"]=self.config.get(RID,"mark5")
        runes["mark6"]=self.config.get(RID,"mark6")
        runes["mark7"]=self.config.get(RID,"mark7")
        runes["mark8"]=self.config.get(RID,"mark8")
        runes["mark9"]=self.config.get(RID,"mark9")

        runes["seal1"]=self.config.get(RID,"seal1")
        runes["seal2"]=self.config.get(RID,"seal2")
        runes["seal3"]=self.config.get(RID,"seal3")
        runes["seal4"]=self.config.get(RID,"seal4")
        runes["seal5"]=self.config.get(RID,"seal5")
        runes["seal6"]=self.config.get(RID,"seal6")
        runes["seal7"]=self.config.get(RID,"seal7")
        runes["seal8"]=self.config.get(RID,"seal8")
        runes["seal9"]=self.config.get(RID,"seal9")
        
        runes["glyph1"]=self.config.get(RID,"glyph1")
        runes["glyph2"]=self.config.get(RID,"glyph2")
        runes["glyph3"]=self.config.get(RID,"glyph3")
        runes["glyph4"]=self.config.get(RID,"glyph4")
        runes["glyph5"]=self.config.get(RID,"glyph5")
        runes["glyph6"]=self.config.get(RID,"glyph6")
        runes["glyph7"]=self.config.get(RID,"glyph7")
        runes["glyph8"]=self.config.get(RID,"glyph8")
        runes["glyph9"]=self.config.get(RID,"glyph9")

        runes["quintessence1"]=self.config.get(RID,"quintessence1")
        runes["quintessence2"]=self.config.get(RID,"quintessence2")
        runes["quintessence3"]=self.config.get(RID,"quintessence3")
        
        runes['RID']=RID
        runes['position']=self.config.get(RID,"position")
        runes['name']=self.config.get(RID,"name")
        self.CurrentRID=RID
        return runes

class RecommendedDataModel(QtCore.QAbstractListModel):
    def __init__(self):
        super(RecommendedDataModel,self).__init__()
        self.config=ConfigParser.SafeConfigParser()
        print "RecommendeddataModel Created"
        self.config.read("saves/recommended.sav")
        self.CurrentRecommended=""
        self.CurrentRCID=""
        self.UpdateRecommendedIndex()

    def UpdateRecommendedIndex(self):
        RecommendedList={}
        self.RecommendedIndex=[]
        for i in self.config.sections():
            RecommendedList[int(self.config.get(i,"position"))]=i
        for i in sorted(RecommendedList.keys()):
            self.RecommendedIndex.append(RecommendedList[i])

    def UpdatePosition(self):
        pos = 1
        for i in self.RecommendedIndex:
            self.config.set(i,"position",str(pos))
            pos +=1
        self.saveConfig()

    def rowCount(self,parent = QtCore.QModelIndex()):
        return len(self.config.sections())


    def data(self,index,role):
        if role == QtCore.Qt.DisplayRole:
            RCID=self.RecommendedIndex[index.row()]
            return self.config.get(RCID,"title")

    def flags(self, index):
        return (QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable)

    def insertRows(self,position,rows,name,parent = QtCore.QModelIndex()):
        self.beginInsertRows(parent, position, position + rows - 1)
        RCID = GenerateRandomString()
        position = self.getLastPosition()
        self.CurrentRCID=RCID
        self.config.add_section(RCID)
        self.config.set(RCID,"position",str(position))
        self.config.set(RCID,"title",str(name))
        self.config.set(RCID,"champion","None")
        self.config.set(RCID,"type","custom")
        self.config.set(RCID,"map","any")
        self.config.set(RCID,"priority","true")
        self.config.set(RCID,"section1name","starting")
        self.config.set(RCID,"section2name","essential")
        self.config.set(RCID,"section3name","offensive")
        self.config.set(RCID,"section4name","defensive")
        
        self.config.set(RCID,"section1item1","None")
        self.config.set(RCID,"section1item2","None")
        self.config.set(RCID,"section1item3","None")
        self.config.set(RCID,"section1item4","None")
        self.config.set(RCID,"section1item5","None")
        self.config.set(RCID,"section1item6","None")
        self.config.set(RCID,"section1item7","None")
        self.config.set(RCID,"section1item8","None")
        
        self.config.set(RCID,"section2item1","None")
        self.config.set(RCID,"section2item2","None")
        self.config.set(RCID,"section2item3","None")
        self.config.set(RCID,"section2item4","None")
        self.config.set(RCID,"section2item5","None")
        self.config.set(RCID,"section2item6","None")
        self.config.set(RCID,"section2item7","None")
        self.config.set(RCID,"section2item8","None")
        
        self.config.set(RCID,"section3item1","None")
        self.config.set(RCID,"section3item2","None")
        self.config.set(RCID,"section3item3","None")
        self.config.set(RCID,"section3item4","None")
        self.config.set(RCID,"section3item5","None")
        self.config.set(RCID,"section3item6","None")
        self.config.set(RCID,"section3item7","None")
        self.config.set(RCID,"section3item8","None")
        
        self.config.set(RCID,"section4item1","None")
        self.config.set(RCID,"section4item2","None")
        self.config.set(RCID,"section4item3","None")
        self.config.set(RCID,"section4item4","None")
        self.config.set(RCID,"section4item5","None")
        self.config.set(RCID,"section4item6","None")
        self.config.set(RCID,"section4item7","None")
        self.config.set(RCID,"section4item8","None")
        
        self.config.set(RCID,"section1count1","1")
        self.config.set(RCID,"section1count2","1")
        self.config.set(RCID,"section1count3","1")
        self.config.set(RCID,"section1count4","1")
        self.config.set(RCID,"section1count5","1")
        self.config.set(RCID,"section1count6","1")
        self.config.set(RCID,"section1count7","1")
        self.config.set(RCID,"section1count8","1")
        
        self.config.set(RCID,"section2count1","1")
        self.config.set(RCID,"section2count2","1")
        self.config.set(RCID,"section2count3","1")
        self.config.set(RCID,"section2count4","1")
        self.config.set(RCID,"section2count5","1")
        self.config.set(RCID,"section2count6","1")
        self.config.set(RCID,"section2count7","1")
        self.config.set(RCID,"section2count8","1")
        
        self.config.set(RCID,"section3count1","1")
        self.config.set(RCID,"section3count2","1")
        self.config.set(RCID,"section3count3","1")
        self.config.set(RCID,"section3count4","1")
        self.config.set(RCID,"section3count5","1")
        self.config.set(RCID,"section3count6","1")
        self.config.set(RCID,"section3count7","1")
        self.config.set(RCID,"section3count8","1")
        
        self.config.set(RCID,"section4count1","1")
        self.config.set(RCID,"section4count2","1")
        self.config.set(RCID,"section4count3","1")
        self.config.set(RCID,"section4count4","1")
        self.config.set(RCID,"section4count5","1")
        self.config.set(RCID,"section4count6","1")
        self.config.set(RCID,"section4count7","1")
        self.config.set(RCID,"section4count8","1")
        
        self.endInsertRows()
        self.saveConfig()
        self.UpdateRecommendedIndex()
        return True

    def getLastPosition(self):
        return len(self.RecommendedIndex)+1

    def removeRows(self, position, rows, parent = QtCore.QModelIndex()):
        self.beginRemoveRows(parent, position, position + rows - 1)
        RCID=self.RecommendedIndex[position]
        self.config.remove_section(RCID)
        del self.RecommendedIndex[position]
        self.endRemoveRows()
        self.UpdatePosition()
        return True

    def getRCID(self,pos):
        return self.RecommendedIndex[pos]

    def setData(self, index, value, role = QtCore.Qt.EditRole):
        if role == QtCore.Qt.EditRole:
            #row = index.row()
            #name=value
            RCID=self.RecommendedIndex[index.row()]
            self.config.set(RCID,"title",str(value.toString()))
            self.saveConfig()
            return True
        return False

    def saveConfig(self):
        FILE=open("saves/recommended.sav","w")
        self.config.write(FILE)
        FILE.close()
        print "SavedDataStore: saveConfig: Config written: Recommended.sav"

    def saveRecommended(self,Recommended):
        print "SavedDataStore: saveRecommended: 'Recommended': ",Recommended
        for key,value in sorted(Recommended.iteritems()):
            if value:
                self.config.set(self.CurrentRCID,key,str(value))
            else:
                self.config.set(self.CurrentRCID,key,"None")
        self.saveConfig()

    def loadRecommended(self,RCID):
        Recommended = {}
        Recommended["champion"]=self.config.get(RCID,"champion")
        Recommended["type"]=self.config.get(RCID,"type")
        Recommended["map"]=self.config.get(RCID,"map")
        Recommended["priority"]=self.config.get(RCID,"priority")
        Recommended["section1name"]=self.config.get(RCID,"section1name")
        Recommended["section2name"]=self.config.get(RCID,"section2name")
        Recommended["section3name"]=self.config.get(RCID,"section3name")
        Recommended["section4name"]=self.config.get(RCID,"section4name")
        #Recommended['RCID']=RCID
        Recommended['position']=self.config.get(RCID,"position")
        Recommended['title']=self.config.get(RCID,"title")
        
        Recommended["section1item1"]=self.config.get(RCID,"section1item1")
        Recommended["section1item2"]=self.config.get(RCID,"section1item2")
        Recommended["section1item3"]=self.config.get(RCID,"section1item3")
        Recommended["section1item4"]=self.config.get(RCID,"section1item4")
        Recommended["section1item5"]=self.config.get(RCID,"section1item5")
        Recommended["section1item6"]=self.config.get(RCID,"section1item6")
        Recommended["section1item7"]=self.config.get(RCID,"section1item7")
        Recommended["section1item8"]=self.config.get(RCID,"section1item8")
        
        Recommended["section2item1"]=self.config.get(RCID,"section2item1")
        Recommended["section2item2"]=self.config.get(RCID,"section2item2")
        Recommended["section2item3"]=self.config.get(RCID,"section2item3")
        Recommended["section2item4"]=self.config.get(RCID,"section2item4")
        Recommended["section2item5"]=self.config.get(RCID,"section2item5")
        Recommended["section2item6"]=self.config.get(RCID,"section2item6")
        Recommended["section2item7"]=self.config.get(RCID,"section2item7")
        Recommended["section2item8"]=self.config.get(RCID,"section2item8")
        
        Recommended["section3item1"]=self.config.get(RCID,"section3item1")
        Recommended["section3item2"]=self.config.get(RCID,"section3item2")
        Recommended["section3item3"]=self.config.get(RCID,"section3item3")
        Recommended["section3item4"]=self.config.get(RCID,"section3item4")
        Recommended["section3item5"]=self.config.get(RCID,"section3item5")
        Recommended["section3item6"]=self.config.get(RCID,"section3item6")
        Recommended["section3item7"]=self.config.get(RCID,"section3item7")
        Recommended["section3item8"]=self.config.get(RCID,"section3item8")
        
        Recommended["section4item1"]=self.config.get(RCID,"section4item1")
        Recommended["section4item2"]=self.config.get(RCID,"section4item2")
        Recommended["section4item3"]=self.config.get(RCID,"section4item3")
        Recommended["section4item4"]=self.config.get(RCID,"section4item4")
        Recommended["section4item5"]=self.config.get(RCID,"section4item5")
        Recommended["section4item6"]=self.config.get(RCID,"section4item6")
        Recommended["section4item7"]=self.config.get(RCID,"section4item7")
        Recommended["section4item8"]=self.config.get(RCID,"section4item8")

        Recommended["section1count1"]=self.config.get(RCID,"section1count1")
        Recommended["section1count2"]=self.config.get(RCID,"section1count2")
        Recommended["section1count3"]=self.config.get(RCID,"section1count3")
        Recommended["section1count4"]=self.config.get(RCID,"section1count4")
        Recommended["section1count5"]=self.config.get(RCID,"section1count5")
        Recommended["section1count6"]=self.config.get(RCID,"section1count6")
        Recommended["section1count7"]=self.config.get(RCID,"section1count7")
        Recommended["section1count8"]=self.config.get(RCID,"section1count8")
        
        Recommended["section2count1"]=self.config.get(RCID,"section2count1")
        Recommended["section2count2"]=self.config.get(RCID,"section2count2")
        Recommended["section2count3"]=self.config.get(RCID,"section2count3")
        Recommended["section2count4"]=self.config.get(RCID,"section2count4")
        Recommended["section2count5"]=self.config.get(RCID,"section2count5")
        Recommended["section2count6"]=self.config.get(RCID,"section2count6")
        Recommended["section2count7"]=self.config.get(RCID,"section2count7")
        Recommended["section2count8"]=self.config.get(RCID,"section2count8")
        
        Recommended["section3count1"]=self.config.get(RCID,"section3count1")
        Recommended["section3count2"]=self.config.get(RCID,"section3count2")
        Recommended["section3count3"]=self.config.get(RCID,"section3count3")
        Recommended["section3count4"]=self.config.get(RCID,"section3count4")
        Recommended["section3count5"]=self.config.get(RCID,"section3count5")
        Recommended["section3count6"]=self.config.get(RCID,"section3count6")
        Recommended["section3count7"]=self.config.get(RCID,"section3count7")
        Recommended["section3count8"]=self.config.get(RCID,"section3count8")
        
        Recommended["section4count1"]=self.config.get(RCID,"section4count1")
        Recommended["section4count2"]=self.config.get(RCID,"section4count2")
        Recommended["section4count3"]=self.config.get(RCID,"section4count3")
        Recommended["section4count4"]=self.config.get(RCID,"section4count4")
        Recommended["section4count5"]=self.config.get(RCID,"section4count5")
        Recommended["section4count6"]=self.config.get(RCID,"section4count6")
        Recommended["section4count7"]=self.config.get(RCID,"section4count7")
        Recommended["section4count8"]=self.config.get(RCID,"section4count8")
        
        self.CurrentRCID=RCID
        return Recommended
